home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9755 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  106 lines

  1. Path: news.duke.edu!agate!nickkral
  2. From: nickkral@america.CS.Berkeley.EDU (Nick Kralevich)
  3. Newsgroups: comp.os.linux.misc,comp.lang.c
  4. Subject: Re: Two things, RedHat + Linux efficiency
  5. Date: 13 Mar 1996 05:11:59 GMT
  6. Organization: Electrical Engineering Computer Science Department, University of California at Berkeley
  7. Message-ID: <4i5lev$fd3@agate.berkeley.edu>
  8. References: <4lFXuqq00aw=M=iUNp@andrew.cmu.edu>
  9. NNTP-Posting-Host: america.cs.berkeley.edu
  10.  
  11. In article <4lFXuqq00aw=M=iUNp@andrew.cmu.edu>,
  12. Nicholas P Konidaris  <npk+@andrew.cmu.edu> wrote:
  13. >Ok, these are two totally unrelated questions/comments..  I was coding
  14. >around on my box, and wanted to see the speed difference (my CS prof
  15. >told me that x++ is faster than x = x + 1)  So, I write code that looks
  16. >like:
  17.  
  18. The appropriate place for this is comp.lang.c, but oh well.  I've 
  19. added comp.lang.c to the newsgroup line.
  20.  
  21. On some instruction sets, there is actually an "incriment" instruction
  22. built into the hardware.  I'm not sure if this is true with the
  23. intel platform.  If this is the case, then the one incriment instruction
  24. would be faster than having to do a load, add, and store (3 instructions).
  25.  
  26. >......
  27. >for (cnt = 0; cnt < 50000; cnt++)
  28. >    x = x + 1
  29. >......
  30. >and
  31. >......
  32. >for (cnt = 0; cnt < 50000; cnt++)
  33. >    x++
  34. >......
  35.  
  36. However, in the case here, it shouldn't make a difference.  That is
  37. because you only need 1 load, 1 store for the outside of the loop,
  38. then the rest of the iterations could be done in registers.
  39.  
  40. Note 1:  There's a bug in your program.  "x" isn't properly initialized. :)
  41.  
  42. Note 2:  If you were to run "gcc -O2 blah...", that should reduce the
  43. loop to:
  44.  
  45.     cnt = 50000;
  46.     x = 50000;
  47.  
  48. Since the value of x and cnt can be computed at compile time.
  49.  
  50. >I use the  good 'ol 'time' command, and get pretty much the exact same
  51. >time to do both those computations.  Now, I substitute the x++ with x--
  52. >and for some really odd reason, the x-- is slower.... Is this a gcc
  53. >thing, or a linux thing or what :)  Any pointers/suggestions of why this
  54. >happens would be appreciated :)
  55.  
  56. My understanding is that there should be no speed difference between
  57. x++ and x--.  (Of course, I'm not an expert on the intel platform, so
  58. someone should correct me if I'm wrong).
  59.  
  60. Here's a couple of things that could have gone wrong with your testing:
  61.  
  62. 50000 loops is too few to time with any accuracy.  The overhead
  63. of loading in shared libraries, initializing the program, scheduling
  64. problems, caching, etc...  all would have degraded the accuracy of your 
  65. timing measurements.  Ideally, you would compile your program with:
  66.  
  67.    gcc -O2 -fomit-frame-pointer -static timecount.c -o timecount
  68.  
  69. to get good numbers.  The "-static" is the most important, because it 
  70. eliminates the overhead of bringing in shared libraries, at the expense
  71. of making your executable size bigger.
  72.  
  73. Also, you probibly want to put the timing code in your code, instead
  74. of relying on an external program ("time") to time your program.  
  75. So your program would look somthing like:
  76.  
  77. main()
  78. {
  79.   double seconds1,seconds2;
  80.   int count,i;
  81.   seconds1 = get_seconds();
  82.   i = 0;
  83.   for(count = 0; count < 50000; count++) {
  84.     i++;
  85.   }
  86.   seconds2 = get_seconds();
  87.   printf("Total time = %f\n", seconds2 - seconds1);
  88. }
  89.  
  90. Where "get_seconds()" is a high performance timer call.
  91.  
  92. Anyway, I think I've gone off the deep end here, and I don't know
  93. if I really answered your question or not.  There shouldn't
  94. be any difference in speed between "x++", "x--", and "x = x + 1" on
  95. modern computers.  Only computers that have an incriment or decriment
  96. instruction might show improvements, but even that I'm not
  97. sure about.
  98.  
  99. Perhaps someone has an intel instruction guide listing the available
  100. instructions, and the time they take to execute.
  101.  
  102. Take care,
  103. -- Nick Kralevich
  104.    nickkral@cory.eecs.berkeley.edu
  105.  
  106.